package com.aat.dxfy.utils;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

public class IOUtil {

	public static byte[] file2byte(File file) {
		try {
			FileInputStream fis = new FileInputStream(file);
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			byte[] b = new byte[1024];
			int len = -1;
			while ((len = fis.read(b)) != -1) {
				bos.write(b, 0, len);
			}
			byte[] bs = bos.toByteArray();
			bos.close();
			fis.close();
			return bs;
		} catch (Exception e) {

		}
		return null;
	}

	public static byte[] file2byte(String file) {
		try {
			FileInputStream fis = new FileInputStream(file);
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			byte[] b = new byte[1024];
			int len = -1;
			while ((len = fis.read(b)) != -1) {
				bos.write(b, 0, len);
			}
			byte[] bs = bos.toByteArray();
			bos.close();
			fis.close();
			return bs;
		} catch (Exception e) {

		}
		return null;
	}

	public static byte[] imageConverterJpeg(InputStream input) {
		try {
			BufferedImage bi = ImageIO.read(input);
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			boolean b1 = ImageIO.write(bi, "jpeg", out);
			if (b1) {
				return out.toByteArray();
			}
			return null;
		} catch (IOException e) {
		}
		return null;

	}
	
	public static byte[] imageConverter(InputStream input,String format) {
		try {
			BufferedImage bi = ImageIO.read(input);
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			boolean b1 = ImageIO.write(bi, format, out);
			if (b1) {
				return out.toByteArray();
			}
			return null;
		} catch (IOException e) {
		}
		return null;

	}

}
